package com.xbongbong.paas.lowcode.impl;

import com.xbongbong.paas.lowcode.CustomButtonService;
import com.xbongbong.paas.lowcode.LowCodeActionSettingService;
import com.xbongbong.paas.lowcode.LowCodeConfigService;
import com.xbongbong.paas.lowcode.LowCodeJsFileService;
import com.xbongbong.paas.lowcode.PaasFormServiceRegistrationService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.actionsetting.pojo.dto.LowCodeActionSettingAddDTO;
import com.xbongbong.pro.actionsetting.pojo.dto.LowCodeActionSettingListDTO;
import com.xbongbong.pro.actionsetting.pojo.vo.LowCodeActionSettingListVO;
import com.xbongbong.pro.custombutton.pojo.dto.CustomButtonListDTO;
import com.xbongbong.pro.custombutton.pojo.dto.CustomButtonSaveDTO;
import com.xbongbong.pro.custombutton.pojo.vo.CustomButtonListVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.pro.formservice.pojo.dto.FormServiceListDTO;
import com.xbongbong.pro.formservice.pojo.dto.FormServiceSaveDTO;
import com.xbongbong.pro.formservice.pojo.vo.FormServiceListVO;
import com.xbongbong.pro.lowcodeconfig.pojo.dto.LowCodeConfigGetDTO;
import com.xbongbong.pro.lowcodeconfig.pojo.dto.LowCodeConfigListDTO;
import com.xbongbong.pro.lowcodeconfig.pojo.dto.LowCodeConfigSaveDTO;
import com.xbongbong.pro.lowcodeconfig.pojo.vo.LowCodeConfigGetVO;
import com.xbongbong.pro.lowcodeconfig.pojo.vo.LowCodeConfigListVO;
import com.xbongbong.pro.lowcodeconfig.pojo.vo.LowCodeConfigSaveVO;
import com.xbongbong.pro.lowcodejsfile.pojo.LowCodeJsFilePojo;
import com.xbongbong.pro.lowcodejsfile.pojo.dto.LowCodeJsFileGetDTO;
import com.xbongbong.pro.lowcodejsfile.pojo.vo.LowCodeJsFileGetVO;
import com.xbongbong.sys.help.ProPermissionHelp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 吴峰
 * @date 2021/03/09 17:19
 */
@Service("lowCodeConfigService")
public class LowCodeConfigServiceImpl implements LowCodeConfigService {

    private static final Logger LOG = LoggerFactory.getLogger(LowCodeConfigServiceImpl.class);

    @Resource
    private CustomButtonService customButtonService;
    @Resource
    private PaasFormServiceRegistrationService paasFormServiceRegistrationService;
    @Resource
    private LowCodeActionSettingService lowCodeActionSettingService;
    @Resource
    private LowCodeJsFileService lowCodeJsFileService;

    @Override
    public LowCodeConfigListVO list(LowCodeConfigListDTO lowCodeConfigListDTO) throws XbbException {
        LowCodeConfigListVO lowCodeConfigListVO = new LowCodeConfigListVO();
        try {
            UserVO userVO = lowCodeConfigListDTO.getLoginUser();
            String corpid = lowCodeConfigListDTO.getCorpid();
            Integer saasMark = lowCodeConfigListDTO.getSaasMark();
            Integer businessType = lowCodeConfigListDTO.getBusinessType();
            Long formId = lowCodeConfigListDTO.getFormId();
            LowCodeActionSettingListDTO lowCodeActionSettingListDTO = new LowCodeActionSettingListDTO();
            lowCodeActionSettingListDTO.setCorpid(corpid);
            lowCodeActionSettingListDTO.setBusinessType(businessType);
            lowCodeActionSettingListDTO.setSaasMark(saasMark);
            lowCodeActionSettingListDTO.setFormId(formId);
            lowCodeActionSettingListDTO.setLoginUser(userVO);
            LowCodeActionSettingListVO lowCodeActionSettingListVO = lowCodeActionSettingService.fieldList(lowCodeActionSettingListDTO);
            lowCodeConfigListVO.setActionList(lowCodeActionSettingListVO.getActionList());
            // 获取服务注册
            FormServiceListDTO formServiceListDTO = new FormServiceListDTO();
            formServiceListDTO.setBusinessType(businessType);
            formServiceListDTO.setCorpid(corpid);
            formServiceListDTO.setSaasMark(saasMark);
            formServiceListDTO.setFormId(formId);
            FormServiceListVO formServiceListVO = paasFormServiceRegistrationService.list(formServiceListDTO);
            lowCodeConfigListVO.setServiceList(formServiceListVO.getServiceList());
            // 自定义按钮
            CustomButtonListDTO customButtonListDTO = new CustomButtonListDTO();
            customButtonListDTO.setBusinessType(businessType);
            customButtonListDTO.setCorpid(corpid);
            customButtonListDTO.setSaasMark(saasMark);
            customButtonListDTO.setFormId(formId);
            CustomButtonListVO customButtonListVO= customButtonService.list(customButtonListDTO);
            lowCodeConfigListVO.setCustomButtonList(customButtonListVO.getCustomButtonList());
            LowCodeJsFileGetDTO lowCodeJsFileGetDTO = new LowCodeJsFileGetDTO();
            lowCodeJsFileGetDTO.setCorpid(corpid);
            lowCodeJsFileGetDTO.setBusinessType(businessType);
            lowCodeJsFileGetDTO.setSaasMark(saasMark);
            lowCodeJsFileGetDTO.setFormId(formId);
            LowCodeJsFileGetVO lowCodeJsFileGetVO = lowCodeJsFileService.get(lowCodeJsFileGetDTO);
            LowCodeJsFilePojo lowCodeJsFile = new LowCodeJsFilePojo(lowCodeJsFileGetVO.getFileId(), lowCodeJsFileGetVO.getWhetherToPublish(), lowCodeJsFileGetVO.getRelease(),
                    lowCodeJsFileGetVO.getUnreleased());
            lowCodeConfigListVO.setLowCodeJsFile(lowCodeJsFile);
        } catch (Exception e) {
            LOG.error("LowCodeConfigServiceImpl.list 出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return lowCodeConfigListVO;
    }


    @Override
    public LowCodeConfigSaveVO save(LowCodeConfigSaveDTO lowCodeConfigSaveDTO) throws XbbException {
        LowCodeConfigSaveVO lowCodeConfigSaveVO = new LowCodeConfigSaveVO();
        try {
            UserVO userVO = lowCodeConfigSaveDTO.getLoginUser();
            ProPermissionHelp.verifyPermission(userVO, ProPermissionAliasEnum.FORM_SET_UPDATE.getAlias());
            String corpid = lowCodeConfigSaveDTO.getCorpid();
            Long formId = lowCodeConfigSaveDTO.getFormId();
            Integer saasMark = lowCodeConfigSaveDTO.getSaasMark();
            String userId = lowCodeConfigSaveDTO.getUserId();
            String loginUserName = lowCodeConfigSaveDTO.getLoginUserName();
            Integer businessType = lowCodeConfigSaveDTO.getBusinessType();
            // 表单服务
            FormServiceSaveDTO formServiceSaveDTO = new FormServiceSaveDTO();
            formServiceSaveDTO.setBusinessType(businessType);
            formServiceSaveDTO.setCorpid(corpid);
            formServiceSaveDTO.setSaasMark(saasMark);
            formServiceSaveDTO.setFormId(formId);
            formServiceSaveDTO.setLoginUser(userVO);
            formServiceSaveDTO.setUserId(userId);
            formServiceSaveDTO.setLoginUserName(loginUserName);
            formServiceSaveDTO.setFormServiceList(lowCodeConfigSaveDTO.getFormServiceList());
            paasFormServiceRegistrationService.save(formServiceSaveDTO);
            // 表单按钮
            CustomButtonSaveDTO customButtonSaveDTO = new CustomButtonSaveDTO();
            customButtonSaveDTO.setBusinessType(businessType);
            customButtonSaveDTO.setCorpid(corpid);
            customButtonSaveDTO.setSaasMark(saasMark);
            customButtonSaveDTO.setFormId(formId);
            customButtonSaveDTO.setLoginUser(userVO);
            customButtonSaveDTO.setUserId(userId);
            customButtonSaveDTO.setLoginUserName(loginUserName);
            customButtonSaveDTO.setCustomButtonList(lowCodeConfigSaveDTO.getCustomButtonList());
            customButtonService.add(customButtonSaveDTO);
            // 字段动作
            LowCodeActionSettingAddDTO lowCodeActionSettingAddDTO = new LowCodeActionSettingAddDTO();
            lowCodeActionSettingAddDTO.setBusinessType(businessType);
            lowCodeActionSettingAddDTO.setCorpid(corpid);
            lowCodeActionSettingAddDTO.setSaasMark(saasMark);
            lowCodeActionSettingAddDTO.setFormId(formId);
            lowCodeActionSettingAddDTO.setLoginUser(userVO);
            lowCodeActionSettingAddDTO.setUserId(userId);
            lowCodeActionSettingAddDTO.setLoginUserName(loginUserName);
            lowCodeActionSettingAddDTO.setLowCodeActionSettingList(lowCodeConfigSaveDTO.getLowCodeActionSettingList());
            lowCodeActionSettingService.add(lowCodeActionSettingAddDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("LowCodeConfigServiceImpl.save 出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return lowCodeConfigSaveVO;
    }

    @Override
    public LowCodeConfigGetVO get(LowCodeConfigGetDTO lowCodeConfigGetDTO) throws XbbException {
        LowCodeConfigGetVO lowCodeConfigGetVO = new LowCodeConfigGetVO();
        UserVO userVO = lowCodeConfigGetDTO.getLoginUser();
        String corpid = lowCodeConfigGetDTO.getCorpid();
        Integer saasMark = lowCodeConfigGetDTO.getSaasMark();
        Integer businessType = lowCodeConfigGetDTO.getBusinessType();
        Long formId = lowCodeConfigGetDTO.getFormId();
        LowCodeActionSettingListDTO lowCodeActionSettingListDTO = new LowCodeActionSettingListDTO();
        lowCodeActionSettingListDTO.setBusinessType(businessType);
        lowCodeActionSettingListDTO.setCorpid(corpid);
        lowCodeActionSettingListDTO.setSaasMark(saasMark);
        lowCodeActionSettingListDTO.setFormId(formId);
        lowCodeActionSettingListDTO.setLoginUser(userVO);
        LowCodeActionSettingListVO lowCodeActionSettingListVO = lowCodeActionSettingService.fieldList(lowCodeActionSettingListDTO);
        lowCodeConfigGetVO.setActionList(lowCodeActionSettingListVO.getActionList());
        LowCodeJsFileGetDTO lowCodeJsFileGetDTO = new LowCodeJsFileGetDTO();
        lowCodeJsFileGetDTO.setBusinessType(businessType);
        lowCodeJsFileGetDTO.setCorpid(corpid);
        lowCodeJsFileGetDTO.setSaasMark(saasMark);
        lowCodeJsFileGetDTO.setFormId(formId);
        LowCodeJsFileGetVO lowCodeJsFileGetVO = lowCodeJsFileService.get(lowCodeJsFileGetDTO);
        LowCodeJsFilePojo lowCodeJsFile = new LowCodeJsFilePojo(lowCodeJsFileGetVO.getFileId(), lowCodeJsFileGetVO.getWhetherToPublish(), lowCodeJsFileGetVO.getRelease(),
                lowCodeJsFileGetVO.getUnreleased());
        lowCodeConfigGetVO.setLowCodeJsFile(lowCodeJsFile);
        CustomButtonListDTO customButtonListDTO = new CustomButtonListDTO();
        customButtonListDTO.setBusinessType(businessType);
        customButtonListDTO.setFormId(formId);
        customButtonListDTO.setSaasMark(saasMark);
        customButtonListDTO.setCorpid(corpid);
        customButtonListDTO.setLoginUser(lowCodeConfigGetDTO.getLoginUser());
        customButtonListDTO.setType(lowCodeConfigGetDTO.getType());
        CustomButtonListVO formCustomButtonListVO = customButtonService.listByPermission(customButtonListDTO);
        lowCodeConfigGetVO.setCustomButtonList(formCustomButtonListVO.getCustomButtonList());
        return lowCodeConfigGetVO;
    }
}
